﻿using MSL.CodeMatic.App_Code;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility.Data;
using Utility.UI;

public partial class Frame_MainForm : PageBase
{
    public StringBuilder strHtml = new StringBuilder();
    public object connectstring = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectstring = Session["connectstring"];
        if (connectstring != null)
        {
            InitDBInfo();
        }
        else
        {
            ShowMsgHelper.ShowScript("LoginForm()");
        }
    }
    /// <summary>
    /// 加载数据库信息
    /// </summary>
    public void InitDBInfo()
    {
        DataTable dt = DBHelper.GetDataBaseName(connectstring.ToString());
        if (Request.QueryString["db"] != null)
        {
            string dbName = Request.QueryString["db"].ToString();
            dt = DataTableHelper.GetNewDataTable(dt, "name='" + dbName + "'");
        }
        if (DataTableHelper.IsExistRows(dt))
        {
            strHtml.Append("<ul class=\"black strTree\">");
            strHtml.Append("    <li>");
            strHtml.Append("        <div><img src=\"../Themes/Images/db.png\" />数据库</div>");
            strHtml.Append("        <ul>");
            DataView dv = new DataView(dt);
            foreach (DataRowView drv in dv)
            {
                strHtml.Append("        <li>");
                strHtml.Append("              <div>");
                strHtml.Append(                     drv["NAME"]);
                strHtml.Append("                 <span style='display:none'>" + drv["NAME"].ToString() + "</span>");
                strHtml.Append("              </div>");
                //创建子节点
                strHtml.Append(InitTableInfo(drv["NAME"].ToString()));
                strHtml.Append("        </li>");
            }

            strHtml.Append("        </ul>");
            strHtml.Append("    </li>");
            strHtml.Append("</ul>");
        }
    }
    /// <summary>
    /// 加载数据表
    /// </summary>
    /// <param name="db">数据库名称</param>
    /// <returns></returns>
    public string InitTableInfo(string db)
    {
        StringBuilder treeNode = new StringBuilder();
        string serverName = Session["ServerName"].ToString();
        string loginName = Session["LoginName"].ToString();
        string password = Session["Password"].ToString();
        DataTable dt = DBHelper.GetAllTables(connectstring.ToString(), db, serverName, loginName, password);

        treeNode.Append("<ul style='display: none;' class='ulcontextmenu'>");
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentId = '0'";
        if (dv.Count > 0)
        {
            foreach (DataRowView drv in dv)
            {
                treeNode.Append("<li>");
                treeNode.Append("   <div><img src=\"../Themes/Images/06.gif\" /><span database='" + db + "' tableRemark='" + drv["Remark"].ToString() + "'>" + drv["Field"].ToString() + "</span></div>");
                treeNode.Append(    InitFieldInfo(drv["ID"].ToString(), dt));
                treeNode.Append("</li>");
            }
        }
        treeNode.Append("</ul>");
        return treeNode.ToString();
    }
    /// <summary>
    /// 加载数据库表字段
    /// </summary>
    /// <param name="table">表名</param>
    /// <param name="dt">表字段</param>
    /// <returns></returns>
    public string InitFieldInfo(string table, DataTable dt)
    {
        StringBuilder treeNode = new StringBuilder();
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentId = '" + table + "'";
        if (dv.Count > 0)
        {
            treeNode.Append("<ul style='display: none;'>");
            treeNode.Append("   <li>");
            treeNode.Append("       <div><img src=\"../Themes/Images/folder-closed.gif\"/>列</div>");
            treeNode.Append("       <ul style='display: none;'>");
            foreach (DataRowView drv in dv)
            {
                treeNode.Append("       <li>");
                treeNode.Append("       <div><img src=\"../Themes/Images/06.gif\"/>" + drv["Field"].ToString() + "</div>");
                treeNode.Append("       </li>");
            }
            treeNode.Append("       </ul>");
            treeNode.Append("   </li>");
            treeNode.Append("</ul>");
        }
        return treeNode.ToString();
    }
}